package com.zsr.chapter09.dao;

import com.zsr.chapter09.bean.User;
import com.zsr.chapter09.utils.QueryRunner;

import java.util.ArrayList;

/**
 * @Author zsr
 * @Description
 * @Date 2021/4/22 20:31
 */
public class UsersDao {
    private QueryRunner queryRunner=new QueryRunner();

    // 添加用户的操作
    public boolean insert(User user) {
        String sql="insert into users (`username`,`password`,`email`,`birthday`) values (?,?,?,?)";
        return queryRunner.update(sql,user.getUsername(),user.getPassword(),user.getEmail(),user.getBirthday());
    }

    // 查询所有的User对象
    public ArrayList<User> findAll() {
        String sql="select * from users";
        return (ArrayList<User>) queryRunner.getList(User.class,sql);
    }

    // 根据id查找指定的user
    public User find(int id) {
        String sql ="select * from users where id= ?";
        return queryRunner.query(User.class,sql,id);
    }

    // 删除用户
    public boolean delete(int id) {
        String sql="delete  from users where id=?";
        return queryRunner.update(sql,id);
    }

    // 修改用户
    public boolean update(User user) {
        String sql ="update users set `username`=?,`password`=?,`email`=?,`birthday`=? where id = ?";
        return queryRunner.update(sql,user.getUsername(),user.getPassword(),user.getEmail(),user.getBirthday(),
                user.getId());
    }

}
